package cn.mhome.merchant.server.system.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.mhome.merchant.dao.common.po.SysUser;
import cn.mhome.merchant.dao.jpa.system.SysUserDao;
import cn.mhome.merchant.entity.common.result.Pager;
import cn.mhome.merchant.entity.system.param.SysUserParam;
import cn.mhome.merchant.entity.system.result.SysUserResult;
import cn.mhome.merchant.server.data.convert.SystemConverter;

/**
 * 系统用户
 * @author lvpeng
 */
@Service
public class SysUserServiceImpl implements SysUserService{

	@Resource
	private SysUserDao sysUserDao;
	
	/**
	 * 查询用户列表
	 */
	public Pager<SysUserResult> findForPage(SysUserParam sysUserParam) {
		Pager<SysUserResult> pager = new Pager<SysUserResult>(sysUserParam.getPage(),Integer.MAX_VALUE); 
		List<SysUser> list = sysUserDao.findAll();
		pager.setTotalResults(list.size());
		List<SysUserResult> data = new ArrayList<SysUserResult>();
		for(SysUser sp : list){
			SysUserResult spr = SystemConverter.convert(sp);
			data.add(spr);
		}
		pager.setData(data);
		
		return pager;
	}

	public SysUserResult checkUserPassword(SysUserParam userParam) {
		SysUserResult user = new SysUserResult();
		SysUser sysUser = sysUserDao.findByNamePwd(userParam.getUsername(),userParam.getPassword());
		if(sysUser != null && sysUser.getUserid() > 0){
			user.setUserId(String.valueOf(sysUser.getUserid()));
			user.setJob(sysUser.getJob());
			user.setUsername(sysUser.getUsername());
		}else{
			user.setMsg("用户名或密码错误");
		}
		return user;
	}

}
